function Task_Agent() {

AgentIntelType     = document.getElementById( "AgentIntelType"     ).value;
AgentType          = document.getElementById( "AgentType"          ).value;
AgentURL           = document.getElementById( "AgentURL"           ).value;

if ( AgentType == "a" ) {

document.getElementById( "AgentDisplayName"   ).disabled = false ;
document.getElementById( "AgentURL"           ).disabled = false ;
document.getElementById( "AgentIntelType"     ).disabled = false ;
document.getElementById( "AgentForumUsername" ).disabled = false ;
document.getElementById( "AgentForumPassword" ).disabled = false ;

}

if ( AgentType == "b" ) {

document.getElementById( "AgentDisplayName"   ).value = "Pimp Agent" ;
document.getElementById( "AgentDisplayName"   ).disabled = true ;

document.getElementById( "AgentURL"           ).value = "http://utopiapimp.com/angelapi.aspx" ;
document.getElementById( "AgentURL"           ).disabled = true ;

document.getElementById( "AgentIntelType"     ).disabled = false ;
document.getElementById( "AgentForumUsername" ).disabled = false ;

document.getElementById( "AgentForumPassword" ).value = "" ;
document.getElementById( "AgentForumPassword" ).disabled = true ;

}

if ( AgentType == "c" ) {

document.getElementById( "AgentDisplayName"   ).value = "StingerNET" ;
document.getElementById( "AgentDisplayName"   ).disabled = true ;

if ( document.getElementById( "AgentURL" ).value == ""                                    ) document.getElementById( "AgentURL" ).value = "http://stingernet.dyndns-server.com/" ;
if ( document.getElementById( "AgentURL" ).value == "http://utopiapimp.com/angelapi.aspx" ) document.getElementById( "AgentURL" ).value = "http://stingernet.dyndns-server.com/" ;

document.getElementById( "AgentURL"           ).disabled = false ;

document.getElementById( "AgentIntelType"     ).value = "b" ;
document.getElementById( "AgentIntelType"     ).disabled = true ;

document.getElementById( "AgentForumUsername" ).value = "StingerNET" ;
document.getElementById( "AgentForumUsername" ).disabled = true ;

document.getElementById( "AgentForumPassword" ).value = "" ;
document.getElementById( "AgentForumPassword" ).disabled = true ;

}

}

function Task_Manager() {
Task_Agent();
setTimeout("Task_Manager()",1);
}

Task_Manager();


//////////////////////////////
//                          //
// AddAgent()               //
// AgentExists(DisplayName) //
// UpdateAgent( ......... ) //
// CheckForEdit()           //
//                          //
//////////////////////////////


function AddAgent() {

AgentDisplayName   = document.getElementById( "AgentDisplayName"   ).value;
AgentIntelType     = document.getElementById( "AgentIntelType"     ).value;
AgentType          = document.getElementById( "AgentType"          ).value;
AgentURL           = document.getElementById( "AgentURL"           ).value;
AgentUsername      = document.getElementById( "AgentUsername"      ).value;
AgentPassword      = document.getElementById( "AgentPassword"      ).value;
AgentForumUsername = document.getElementById( "AgentForumUsername" ).value;
AgentForumPassword = document.getElementById( "AgentForumPassword" ).value;

if ( AgentDisplayName   == "") { alert( 'The display name must not be blank!' ); return false; }
if ( AgentURL           == "") { alert( 'The url must not be blank!'          ); return false; }
if ( AgentUsername      == "") { alert( 'The username must not be blank!'     ); return false; }
if ( AgentPassword      == "") { alert( 'The password must not be blank!'     ); return false; }
if ( AgentForumUsername == "") { alert( 'The forum name must not be blank!'   ); return false; }

if ( AgentDisplayName.indexOf(' #######')   !== -1 || AgentDisplayName.indexOf(' |||||||')   !== -1 ) { alert( 'Display name must not contain hash or pipe symbols!'  ); return false; }
if ( AgentURL.indexOf(' #######')           !== -1 || AgentURL.indexOf(' |||||||')           !== -1 ) { alert( 'Url must not contain hash or pipe symbols!'           ); return false; }
if ( AgentUsername.indexOf(' #######')      !== -1 || AgentUsername.indexOf(' |||||||')      !== -1 ) { alert( 'Username must not contain hash or pipe symbols!'      ); return false; }
if ( AgentPassword.indexOf(' #######')      !== -1 || AgentPassword.indexOf(' |||||||')      !== -1 ) { alert( 'Your password must not contain hash or pipe symbols!' ); return false; }
if ( AgentForumUsername.indexOf(' #######') !== -1 || AgentForumUsername.indexOf(' |||||||') !== -1 ) { alert( 'Forum name must not contain hash or pipe symbols!'    ); return false; }

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var AgentList = prefService.getCharPref('extensions.Ultima.AgentList');

var exists = AgentExists( AgentDisplayName );

if (exists) {
var response = confirm('This will overwrite the existing agent, proceed?');
if (!response) return false;
if (response) window.close();
}

if (!exists) {

AgentList += " |||||||" + AgentDisplayName + " #######" + AgentIntelType + " #######" + AgentType + " #######" + AgentURL + " #######" + AgentUsername + " #######" + AgentPassword + " #######" + AgentForumUsername + " #######" + AgentForumPassword ;
prefService.setCharPref( 'extensions.Ultima.AgentList' , AgentList );
window.close();

} else {
UpdateAgent( AgentDisplayName , AgentIntelType , AgentType , AgentURL , AgentUsername , AgentPassword , AgentForumUsername , AgentForumPassword );
}

}


function AgentExists(DisplayName) {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var AgentList = prefService.getCharPref('extensions.Ultima.AgentList').split(' |||||||');

for(q=0; q<AgentList.length; q++) { var entry = AgentList[q].split(' #######'); if ( DisplayName == entry[0] ) return true; }
return false;

}


function UpdateAgent( AgentDisplayName , AgentIntelType , AgentType , AgentURL , AgentUsername , AgentPassword , AgentForumUsername , AgentForumPassword ) {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var AgentList = prefService.getCharPref( 'extensions.Ultima.AgentList' ).split(' |||||||');

for(q=0; q<AgentList.length; q++) {

var Entry = AgentList[q].split(' #######');

if ( AgentDisplayName == Entry[0] ) {

Entry[0] = AgentDisplayName ;
Entry[1] = AgentIntelType ;
Entry[2] = AgentType ;
Entry[3] = AgentURL ;
Entry[4] = AgentUsername ;
Entry[5] = AgentPassword ;
Entry[6] = AgentForumUsername ;
Entry[7] = AgentForumPassword ;

var Temp = Entry.join(' #######');

AgentList[q] = Temp;

var Temp_Entry = AgentList.join(' |||||||');

prefService.setCharPref( 'extensions.Ultima.AgentList' , Temp_Entry );

}

}

}


function CheckForEdit() {

if ( window.arguments[0] !== '' ) {

var Params = window.arguments[0];

document.getElementById( "AgentDisplayName"   ).value = Params[0]
document.getElementById( "AgentIntelType"     ).value = Params[1]
document.getElementById( "AgentType"          ).value = Params[2]
document.getElementById( "AgentURL"           ).value = Params[3]
document.getElementById( "AgentUsername"      ).value = Params[4]
document.getElementById( "AgentPassword"      ).value = Params[5]
document.getElementById( "AgentForumUsername" ).value = Params[6]
document.getElementById( "AgentForumPassword" ).value = Params[7]

}

}
